﻿Imports System.Security.Principal

Module Module1
    Dim identity = WindowsIdentity.GetCurrent()
    Dim principal = New WindowsPrincipal(identity)
    Dim isElevated As Boolean = principal.IsInRole(WindowsBuiltInRole.Administrator)

    Dim WPIlib As New wpi_lib.packages.cli
    Sub Main()
        If Not isElevated Then
            PrintHelp()
            Exit Sub
        End If
        Dim A() As String = System.Environment.GetCommandLineArgs()
        Select Case A.Count
            Case Is = 2
                Select Case A(1).ToLower
                    Case Is = "update", "upd", "u"
                        WPIlib.UpdatePackages()

                    Case Is = "listavaliable", "lstav", "la"
                        WPIlib.ListAvaliablePackages()

                    Case Is = "listinstalled", "lstin", "li"
                        WPIlib.ListInstalledPackages()

                End Select

            Case Is = 3, Is > 3
                Select Case A(1).ToLower
                    Case Is = "install", "inst", "i"
                        Dim ARCH As String
                        If A.Count > 3 Then
                            If A(3) = "32" Or A(3) = "64" Then
                                ARCH = A(3).ToCharArray
                            Else
                                Console.WriteLine("Invalid architecture. Exiting.")
                                Exit Sub
                            End If
                        Else
                            ARCH = "32"
                            Console.WriteLine("Architecture not specified. Assuming 32bit.")
                        End If

                        WPIlib.InstallPackage(A(2), ARCH)

                    Case Is = "remove", "rm", "r"
                        WPIlib.RemovePackage(A(2))

                End Select

            Case Else
                PrintHelp()

        End Select
    End Sub

    Private Sub PrintHelp()
        SetConsoleColors(ConsoleColor.green)
        Console.WriteLine("Windows Package Installer (WPI) - alpha")
        Console.WriteLine("")
        If Not isElevated Then
            SetConsoleColors(ConsoleColor.red)
            Console.WriteLine("WPI must be run in elevated mode!")
            Console.WriteLine("")
        End If
        SetConsoleColors(ConsoleColor.yellow)
        Console.WriteLine("Syntax")
        SetConsoleColors(ConsoleColor.white)
        Console.WriteLine("wpi-pkg install package_name")
        Console.WriteLine("wpi-pkg remove package_name")
        Console.WriteLine("wpi-pkg update")
        Console.WriteLine("wpi-pkg listavaliable")
        Console.WriteLine("wpi-pkg listinstalled")
        Console.WriteLine("")
        SetConsoleColors(ConsoleColor.grey)
    End Sub

End Module
